package com.leonyr.smartipaddemo.photoview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.leonyr.lib.utils.FileUtil;
import com.leonyr.lib.utils.StatusBarUtil;
import com.leonyr.mvvm.act.AbBindActivity;
import com.leonyr.mvvm.util.RxSchedulers;
import com.leonyr.smartipaddemo.R;
import com.leonyr.smartipaddemo.databinding.ActivityPhotoViewBinding;

import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Date： 2020/6/12 0012
 * Time： 13:53
 * Created by xtqb.
 */
public class PhotoViewActivity extends AbBindActivity<PhotViewViewModel, ActivityPhotoViewBinding> {
    private int rotation;
    private ProgressDialog mProgressDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_photo_view;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarUtil.hackStatusBarImage(Binding().getRoot());
        Binding().setFm(this);
        rotation = 0;
    }

    @Override
    protected void onResume() {
        super.onResume();
        intiData();
    }

    private void intiData() {
        if (null == mProgressDialog) {
            mProgressDialog = new ProgressDialog(mCtx);
//            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
        }

        mProgressDialog.show();
        String cropFile = getIntent().getStringExtra("path");
        Observable
                .fromCallable(new Callable<Bitmap>() {
                    @Override
                    public Bitmap call() throws Exception {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        BitmapFactory.decodeFile(cropFile, options);
                        options.inJustDecodeBounds = false;

                        Bitmap selectedBitmap = BitmapFactory.decodeFile(cropFile, options);
                        FileUtil.deleteFile(cropFile);
                        return selectedBitmap;
                    }
                })
                .compose(RxSchedulers.IOMain())
                .subscribe(new Observer<Bitmap>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
//
                        if (bitmap != null) {
                            Binding().photoview.setImageBitmap(bitmap);
                        }

                        mProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mProgressDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    /**
     * 点击拍照按钮
     *
     * @param view
     */
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.action_rotation:
                rotation += 90;
                if (rotation == 360) {
                    rotation = 0;
                }
                Binding().photoview.setRotation(rotation);
                Log.e("Tag", "--------------------");
                break;
            case R.id.action_back:
                Intent intent = new Intent();
                intent.putExtra("value", rotation);
                intent.putExtra("pos", getIntent().getIntExtra("pos", -1));
                setResult(Activity.RESULT_OK, intent);
                this.finish();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
        mProgressDialog = null;
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("value", rotation);
        intent.putExtra("pos", getIntent().getIntExtra("pos", -1));
        setResult(Activity.RESULT_OK, intent);
//        super.onBackPressed();
        this.finish();
    }
}
